<template>
  <div class="app-container">
    <div class="searchContianer" :style="{height:showSearch ? '' : '49px',overflow:'hidden'}"> 
      <div class="searchLeft" :style="{height:showSearch ? '' : '49px',overflow:'hidden'}">
        <el-form :model="queryParams" ref="queryForm" :inline="true" size="small" label-width="68px">

          <el-form-item label="联系人" prop="contactName" class="mb8">
            <el-input
              v-model="queryParams.contactName"
              placeholder="请输入联系人"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="联系电话" prop="contactMobile" class="mb8">
            <el-input
              v-model="queryParams.contactMobile"
              placeholder="请输入联系电话"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="服务类型" prop="serverTypeId" class="mb8">
            <el-input
              v-model="queryParams.serverTypeId"
              placeholder="请输入服务类型"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>  
          <el-form-item label="结算状态" prop="orderSettleState" class="mb8">
            <el-select v-model="queryParams.orderSettleState" placeholder="请选择结算状态" clearable>
              <el-option
                v-for="dict in dict.type.sk_settle_state"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        
        </el-form>
      </div>
      <div class="searchRight">
        <div style="margin-bottom:20px;margin-top:5px">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </div>
      </div>
    </div>

    

    <el-table ref="tables" v-loading="loading" border :data="orderSettleList" @selection-change="handleSelectionChange"
    	 :default-sort="defaultSort" @sort-change="handleSortChange" :height="bigTableData">
      <el-table-column type="selection" width="55" align="center" fixed="left"/>
      <el-table-column label="订单号" align="left" prop="orderNo" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="联系人" align="left" prop="contactName" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="联系电话" align="left" prop="contactMobile" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="产品名称" align="left" prop="customerProductName" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="产品型号" align="left" prop="customerProductModeNo" :show-overflow-tooltip="true" min-width="100"/>

      <el-table-column label="服务类型" align="left" prop="serverTypeName" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="在保状态" align="left" prop="productInServer" :show-overflow-tooltip="true" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sk_yes_no" :value="scope.row.productInServer"/>
        </template>
      </el-table-column>
      <el-table-column label="结算状态" align="left" prop="orderSettleState" :show-overflow-tooltip="true" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sk_settle_state" :value="scope.row.orderSettleState"/>
        </template>
      </el-table-column>
      <el-table-column label="上门费用" align="left" prop="onsiteFee" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="维修费用" align="left" prop="repairFee" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="配件费用" align="left" prop="partFee" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="应收金额" align="left" prop="payableAmount" :show-overflow-tooltip="true" min-width="100"/>
      <el-table-column label="实收金额" align="left" prop="paidAmount" :show-overflow-tooltip="true" min-width="100"/>

      <el-table-column label="创建时间" align="left" prop="orderTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>

  
</template>

<script>
import { listOrderSettle, listUnion, getOrderSettle, getInfoUnion, delOrderSettle, addOrderSettle, updateOrderSettle, exportOrderSettle } from "@/api/vanger/orderSettle";

export default {
  name: "OrderSettle",
  dicts: ['sk_settle_state', 'sk_yes_no', 'sk_order_status'],
  data() {
    return {
      // 表格高度
      bigTableData:'',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 日期范围
      dateRange: [],
      // 订单结算表格数据
      orderSettleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查看详情
      showDetail: false,
      // 默认排序
      defaultSort: {prop: 'createTime', order: 'descending'},
      // 查询参数
      queryParams: {
        params: {},
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        orderNo: null,
        serverTypeId: null,
        serverTypeName: null,
        problemTypeId: null,
        problemTypeName: null,
        customerId: null,
        customeName: null,
        customeMobile: null,
        customerSignedType: null,
        customerTag: null,
        productCategoryId: null,
        productCategoryName: null,
        customerProductId: null,
        customerProductName: null,
        customerProductModeNo: null,
        productInServer: null,
        customerStoreId: null,
        customerStoreName: null,
        contactName: null,
        contactMobile: null,
        onsiteFee: null,
        repairFee: null,
        partFee: null,
        payableAmount: null,
        paidAmount: null,
        engineerId: null,
        engineerName: null,
        engineerMobile: null,
        engineerCommission: null,
        engineerSettleState: null,
        customerSettleState: null,
        orderSettleState: null,
        orderStatus: null,
        orderTime: null,
        isDelete: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.handleSortChange(this.defaultSort);
  },
  mounted() {
    this.bigTableData = document.body.clientHeight - document.getElementsByClassName('searchLeft')[0].offsetHeight - 220 + 'px'
  },
  watch: {
    showSearch() {
      this.$nextTick(function () {
        this.bigTableData = document.body.clientHeight - document.getElementsByClassName('searchLeft')[0].offsetHeight - 220 + 'px'
      })
    }
  },
  methods: {
  
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 查询订单结算列表 */
    getList() {
      this.loading = true;
      listOrderSettle(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.orderSettleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.closeDialog()
    },
	// 关闭时设置
    closeDialog() {
      this.open = false;
      this.showDetail = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        orderNo: null,
        serverTypeId: null,
        serverTypeName: null,
        problemTypeId: null,
        problemTypeName: null,
        customerId: null,
        customeName: null,
        customeMobile: null,
        customerSignedType: null,
        customerTag: null,
        productCategoryId: null,
        productCategoryName: null,
        customerProductId: null,
        customerProductName: null,
        customerProductModeNo: null,
        productInServer: null,
        customerStoreId: null,
        customerStoreName: null,
        contactName: null,
        contactMobile: null,
        onsiteFee: null,
        repairFee: null,
        partFee: null,
        payableAmount: null,
        paidAmount: null,
        engineerId: null,
        engineerName: null,
        engineerMobile: null,
        engineerCommission: null,
        engineerSettleState: null,
        customerSettleState: null,
        orderSettleState: null,
        orderStatus: 0,
        orderTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDelete: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.params = {};
      this.dateRange = [];
      this.resetForm("queryForm");
      // this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order)
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单结算";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrderSettle(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单结算";
      });
    },
    
    /** 查看按钮操作 */
    handleDetail(row) {
      this.reset();
      const id = row.id || this.ids
      getOrderSettle(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看订单结算";
        this.showDetail = true;
      });
    },
    
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrderSettle(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrderSettle(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单结算编号为"' + ids + '"的数据项？').then(function() {
        return delOrderSettle(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('vanger/orderSettle/export', {
        ...this.queryParams
      }, `orderSettle_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
